<template>
  <el-drawer
    direction="rtl"
    custom-class="demo-drawer"
    :title="title"
    ref="drawer"
    size="40%"
    :visible.sync="dialogFormVisible"
    :wrapperClosable="false"
  >
    <div class="demo-drawer__content">
      <el-form ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="书名" prop="bookName">
          <el-input v-model="form.bookName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="作者" prop="bookAuthor">
          <el-input v-model="form.bookAuthor" autocomplete="off" />
        </el-form-item>
        <el-form-item label="封面" prop="bookimg">
          <vab-upload :limit="1" name="file" :size="2" url="/upload" :imgfileList="filelist" @succestList="succestlist" />
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="form.memo" autocomplete="off" type="textarea" :rows="30" />
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button @click="close" type="warning">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
  import VabUpload from '@/components/VabUpload'
  import { SysEditBooks, AddBook } from '@/api/table'

  export default {
    components: {
      VabUpload,
    },
    name: 'TableEdit',
    data() {
      return {
        filelist: [],
        form: {
          bookName: '',
          bookAuthor: '',
          bookImg: '',
        },
        rules: {
          bookName: [{ required: true, trigger: 'blur', message: '请输入书名' }],
          bookAuthor: [{ required: true, trigger: 'blur', message: '请输入作者' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      succestlist(e) {
        this.form.bookImg = e[0].response.data
      },
      showEdit(row) {
        this.filelist = []
        if (!row) {
          this.title = '新增书籍'
        } else {
          this.title = '编辑书籍'
          this.form = Object.assign({}, row)
          this.filelist.push({ url: this.$baseImageUrl + this.form.bookImg })
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetch-data')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = this.title === '新增书籍' ? await AddBook(this.form) : await SysEditBooks(this.form)
            this.$baseMessage(msg, 'success')
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('fetch-data')
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  ::v-deep .demo-drawer__content {
    height: 100%;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    .demo-drawer__footer {
      bottom: 0;
    }
  }
</style>
